grammar GrammarBackup5;

options {
  language = Java;
}

@header {
package com.plt.w2w;
import java.util.Map;
import java.util.HashMap;
import java.io.*;
import java.util.ArrayList;
import java.io.PrintWriter;
import java.text.ParseException;

import java.text.SimpleDateFormat;
}

@lexer::header {
package com.plt.w2w;
}

@members {
  final int TYPE =0;
  final int VALUE =1;
  public Map<String, String[]> symbolTable = new HashMap<String, String[]>();
  public ArrayList<String> mainFuncString = new ArrayList<String>();
  public ArrayList<Garment> garments = new ArrayList<Garment>();
  String date ;
  SimpleDateFormat sdf ;
  java.util.Date checkDate;
  
}

start
  :
  wardrobeCreation mainOptional
    {
//        for(int i=0; i<garments.size(); i++)
//          garments.get(i).printNicely();
        PrintWriter pw;
            try {
              pw = new PrintWriter("TheProgram.java");
              pw.print("package com.plt.w2w; \n");
              pw.print("import java.util.ArrayList;\n");
              pw.print("import java.util.Scanner;\n");
              pw.print("import java.text.SimpleDateFormat;\n");
              pw.print("import java.text.ParseException;\n");
              pw.print("public class TheProgram { \n");
              pw.print("ArrayList<Garment> garments = new ArrayList<Garment>(); \n");
              pw.print("ArrayList<String> _wardrobeNames = new ArrayList<String>(); \n");
              pw.print("String _date;\n");
              pw.print("SimpleDateFormat _sdf = new SimpleDateFormat(\"MM/dd\"); \n");
              pw.print("String _input;\n");
              pw.print($wardrobeCreation.val);
              pw.print("\n");
              pw.print($mainOptional.val);
              pw.print("\npublic static void main(String args[]){\n");  
              pw.print("TheProgram obj = new TheProgram(); \n");    
              pw.print("obj.createWardrobeBackEnd(); \n");
              pw.print("obj.mainBackEnd(); \n");
              pw.print("}\n}");
              pw.close();
            } catch (Exception e) {
              e.printStackTrace();
            }
            
      //call the java class handling wardrobe creation
//      Wardrobe obj = new Wardrobe();
//      obj.create(garments,$wardrobeCreation.val);
      
    }
  | mainProgram
    {
      System.out.println(">>>>>>>>WOHOOOOOO<<<<<<<<<< \n" + $mainProgram.val);
      PrintWriter pw;
            try {
              pw = new PrintWriter("TheProgram.java");
              pw.print("package com.plt.w2w; \n");
              pw.print("import java.util.ArrayList;\n");
              pw.print("import java.util.Scanner;\n");
              pw.print("import java.text.SimpleDateFormat;\n");
              pw.print("import java.text.ParseException;\n");
              pw.print("public class TheProgram {");
              pw.print("ArrayList<Garment> garments = new ArrayList<Garment>(); \n");
              pw.print("String _date;\n");
              pw.print("SimpleDateFormat _sdf = new SimpleDateFormat(\"MM/dd\"); \n");
              pw.print("String _input;\n");
             
              
              pw.print($mainProgram.val);
              pw.print("\n\tpublic static void main(){\n");
              pw.print("mainBackEnd();");
              pw.print("\n\t}\n}");
              pw.close();
            } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            
      MainFuncBackEnd obj = new MainFuncBackEnd(symbolTable,mainFuncString);
      obj.backEnd();
      //call class handling
    }
  ;
  
mainOptional returns [String val]
  :
  mainProgram
  {
      $val = $mainProgram.val;
  }
  |
  {
      $val = "";
  }
  ;

wardrobeCreation returns [String val] 
  :
  createStatement LEFTCURLY items RIGHTCURLY
  {
      String createCode = "";
      for(int i=0;i<garments.size();i++){
          Garment g = (Garment) garments.get(i);
          createCode += "garments.add(new Garment(\"" + g.getType() + "\",\"" + g.getWeight() + "\",\"" + g.getMaker() + "\",\"" +
                                g.getColor() + "\",\"" + g.getShade() + "\",\"" + g.getPreference() + "\"));";
          createCode += "\n";
      }
      $val = "\npublic void createWardrobeBackEnd() {\n" 
      +  createCode +
       "\n" +
       "Wardrobe obj = new Wardrobe(); \n" + 
       "obj.create(garments,\"" + $createStatement.val + "\");\n}"
       ;
  }
  ;

createStatement returns [String val] 
  :
  CREATET WARDROBET wardrobeName ENDOFSTMT   
  {
      $val = $wardrobeName.text;
  }
  ;

items
  :
  item   items1
  {
    garments.add($item.garm);
  }
  ;

items1 
  :
  item  items1
  {
    garments.add($item.garm);
  }
  |
  ;

item returns [Garment garm]
  :
  LEFTCURLY   type category weight color shade maker preference RIGHTCURLY
  {
    Garment g = new Garment($type.val, $weight.val, $maker.val, $color.val, $shade.val, $preference.val);
    $garm = g;
//      String item = "garmemts.add(new Garment(\"" + $type.val + "\",\"" + $weight.val + "\",\"" + $maker.val + "\",\"" +
//                     $color.val + "\",\"" + $shade.val + "\",\"" + $preference.val + "\"));"
//      $garm = item;            
  }
  ;

type returns [String val]
  :
  ITEMT ASSIGNMENT_OP value ENDOFSTMT  
  {
    $val = $value.text;
  }
  ;
  
category returns [String val]
  :
  ITEMT ASSIGNMENT_OP value ENDOFSTMT  
  {
    $val = $value.text;
  }
  |
  {
    $val = "";
  }
  ;
  
weight returns [String val]
  :
  WEIGHT ASSIGNMENT_OP value ENDOFSTMT  
  {
    $val = $value.text;
  }
  |
  {
    $val = "";
  }
  ;
  
color returns [String val]
  :
  COLORT ASSIGNMENT_OP value ENDOFSTMT  
  {
    $val = $value.text;
  }
  |
  {
    $val = "";
  }
  ;

shade returns [String val]
  :
  SHADET ASSIGNMENT_OP value ENDOFSTMT  
  {
    $val = $value.text;
  }
  |
  {
    $val = "";
  }
  ;

maker returns [String val]
  :
  MAKERT ASSIGNMENT_OP value ENDOFSTMT  
  {
    $val = $value.text;
  }
  |
  {
    $val = "";
  }
  ;

preference returns [String val]
  :
  PREFERENCET ASSIGNMENT_OP prefRange ENDOFSTMT  
  {
    $val = $prefRange.text;
  }
  |
  {
    $val = "2";
  }
  ;

mainProgram returns [String val]
  :
  header mainDecl LEFTCURLY lines createEventCommand runCommand RIGHTCURLY
  {
    $val = $mainDecl.val + "{\n" + $header.val +  "Scanner s = new Scanner(System.in);\n"
    + $lines.val + $createEventCommand.val + $runCommand.val + "}";
  }
  ;

header returns [String val]
  :
  USET WARDROBET wardrobeNames ENDOFSTMT  
  {
//    $val = "//Wardrobe to be used: "+ $wardrobeNames.val + "\n";
      $val = "";
    String[] wardrobeNames = $wardrobeNames.val.split(",");
    for(int i = 0; i< wardrobeNames.length;i++){
        $val+= "_wardrobeNames.add(\"" + wardrobeNames[i] + "\"); \n";
    }
  }
  | 
  {
    $val = "_wardrobeNames.add(\"_default\"); \n";
  }
  ;

wardrobeNames returns [String val]
  :
  wardrobeName wardrobeNames1
    {
//       System.out.println("text2: "+$wardrobeName.text);
       $val = $wardrobeName.text + "," + $wardrobeNames1.val ;
       $val = $val.substring(0, $val.length()-1);
    }
  ;

wardrobeNames1 returns [String val]
  :
  '+' wardrobeName w1=wardrobeNames1
  {
//      System.out.println("text: "+$wardrobeName.text);
       $val = $wardrobeName.text + "," + $w1.val;
   }
  | //null
  { $val = "";}
  ;

mainDecl returns [String val]
  :
  VOID MAIN LEFTPARAN RIGHTPARAN  
   {
      $val = "public void mainBackEnd()\n";
   }
  ;


lines returns [String val]
  :
  line l1=lines
  {
    $val = $line.val + "\n" + $l1.val;
  }
  |
  { $val = "" ;}
  ;
  
line returns [String val]
:
   assignmentStatement
   {
    $val = $assignmentStatement.val;
   }
    | 'for each ' GARMENT ' in the ' WARDROBET LEFTCURLY lines RIGHTCURLY
    {
      $val = "for ( Garment g : garmentArrayList ) {\n" + $lines.val + "}";
    }
//  | INCLUDE variableName ENDOFSTMT
    | IF expr LEFTCURLY lines RIGHTCURLY optional_tail
    {
      $val = "if" + $expr.val + "{" + $lines.val + "}" + $optional_tail.val;
    }
    | REJTOK ENDOFSTMT
    {
      $val = "";
    }
    | READT LEFTPARAN RIGHTPARAN ENDOFSTMT
    {
      $val = "s.next();";
    }
    | PRINTT LEFTPARAN printargs RIGHTPARAN ENDOFSTMT
    {
        $val = "System.out.println(" + $printargs.val + ");" ;
    }
//  | RETTOK IDENTIFIER ENDOFSTMT
  ;
  
printargs  returns [String val]
    : printarg printargs1
     {
          $val = $printarg.val + $printargs1.text;  
     }
     ;

printargs1 returns [String val]
        : '+' STRINGTOK p1=printargs1
        {
          $val = $STRINGTOK.text + $p1.val;  
        }
        | '+' variableName p1=printargs1
        {
          if(symbolTable.containsKey($variableName.text))
            $val = " + " + $variableName.text + $p1.val;
          else
            System.out.println("Error! Variable not defined!");
        }
        |
        {
          $val = "";
        }
        ;

printarg  returns [String val]
      : 
      variableName
      {
          $val = $variableName.text ;  
        }
      | STRINGTOK
      {
          $val = $STRINGTOK.text;  
      }
      ;

optional_tail returns [String val]
  : ELSE LEFTCURLY lines RIGHTCURLY
  {
    $val = "else {\n " + $lines.val + "}";
  }
  |
  {
    $val = "";
  }
;  

expr returns [String val]
  :
  conditions
  {
    $val = $conditions.val;
  }
  ;

conditions returns [String val]
  :
  LEFTPARAN c1=conditions beta
  {
    $val = "(" + $c1.val + $beta.val;
  }
  | LEFTPARAN condition RIGHTPARAN
  {
    $val = "(" + $condition.val + ")";
  }
  ;
  
beta returns [String val]
  :
  AND condition RIGHTPARAN
  {
    $val = " && " + $condition.val + ")";
  }
  | OR condition RIGHTPARAN
  {
    $val = " || " + $condition.val + ")";
  }
  ;
  
condition returns [String val]
   :
   n1=NUMBER CONDITIONAL_OP n2=NUMBER
   {
      $val=$n1.text + $CONDITIONAL_OP.text + $n2.text;
   } 
   
   | v1=variableName CONDITIONAL_OP v2=variableName
    {
      if(symbolTable.containsKey($v1.text) && symbolTable.containsKey($v2.text)){
          if(symbolTable.get($v1.text)[TYPE].equals(symbolTable.get($v2.text)[TYPE])){
            if(symbolTable.get($v1.text)[VALUE] != null && symbolTable.get($v2.text)[VALUE] != null){
              if(symbolTable.get($v1.text)[TYPE].equals("int"))
                $val = $v1.text + $CONDITIONAL_OP.text + $v2.text;
              else if (symbolTable.get($v1.text)[TYPE].equals("date")){
                if($CONDITIONAL_OP.text.equals("=="))
                  $val = "(" + $v1.text +  ".compare(\"" + $v2.text + "\")==0";
                else if($CONDITIONAL_OP.text.equals("!="))
                  $val = "(" + $v1.text +  ".compare(\"" + $v2.text + "\")!=0";
                else if($CONDITIONAL_OP.text.equals("<="))
                  $val = "(" + $v1.text +  ".compare(\"" + $v2.text + "\")>=0";
                else if($CONDITIONAL_OP.text.equals(">="))
                  $val = "(" + $v1.text +  ".compare(\"" + $v2.text + "\")<=0";
                else if($CONDITIONAL_OP.text.equals("<"))
                  $val = "(" + $v1.text +  ".compare(\"" + $v2.text + "\")>0";
                else if($CONDITIONAL_OP.text.equals(">"))
                  $val = "(" + $v1.text +  ".compare(\"" + $v2.text + "\")<0";
              }
              else if(symbolTable.get($v1.text)[TYPE].equals("string")){
                if($CONDITIONAL_OP.text.equals("=="))
                  $val = "\"(" + $v1.text +  "\".equalsIgnoreCase(" + $v2.text + ")==0";
                else if($CONDITIONAL_OP.text.equals("!="))
                  $val = "\"(" + $v1.text +  "\".equalsIgnoreCase(" + $v2.text + ")!=0";
                else if($CONDITIONAL_OP.text.equals("<="))
                  $val = "\"(" + $v1.text +  "\".compareToIgnoreCase(" + $v2.text + ")<=0";
                else if($CONDITIONAL_OP.text.equals(">="))
                  $val = "\"(" + $v1.text +  "\".compareToIgnoreCase(" + $v2.text + ")>=0";
                else if($CONDITIONAL_OP.text.equals("<"))
                  $val = "\"(" + $v1.text +  "\".compareToIgnoreCase(" + $v2.text + ")<0";
                else if($CONDITIONAL_OP.text.equals(">"))
                  $val = "\"(" + $v1.text +  "\".compareToIgnoreCase(" + $v2.text + ")>0";
              }
            }
            else {
              System.out.println("Error! No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
    }
  | NUMBER CONDITIONAL_OP variableName
    {
      if(symbolTable.containsKey($variableName.text)){
          if(symbolTable.get($variableName.text)[TYPE].equals("int")){
            if(symbolTable.get($variableName.text)[VALUE] != null){
              $val = $NUMBER.text + $CONDITIONAL_OP.text + $variableName.text;
            }
            else{
               System.out.println("Error! No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
    }
  | variableName CONDITIONAL_OP NUMBER
  {
    if(symbolTable.containsKey($variableName.text)){
          if(symbolTable.get($variableName.text)[TYPE].equals("int")){
            if(symbolTable.get($variableName.text)[VALUE] != null){
              $val = $variableName.text + $CONDITIONAL_OP.text + $NUMBER.text;
            }
            else{
               System.out.println("Error! No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
  }
  | DATE CONDITIONAL_OP variableName
    {
      if(symbolTable.containsKey($variableName.text)){
          if(symbolTable.get($variableName.text)[TYPE].equals("date")){
            if(symbolTable.get($variableName.text)[VALUE] != null){
              if($CONDITIONAL_OP.text.equals("=="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")==0";
            else if($CONDITIONAL_OP.text.equals("!="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")!=0";
            else if($CONDITIONAL_OP.text.equals("<="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")>=0";
            else if($CONDITIONAL_OP.text.equals(">="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")<=0";
            else if($CONDITIONAL_OP.text.equals("<"))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")>0";
            else if($CONDITIONAL_OP.text.equals(">"))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")<0";
            }
            else{
              System.out.println("Error! No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
    }
  | variableName CONDITIONAL_OP DATE
    {
      if(symbolTable.containsKey($variableName.text)){
          if(symbolTable.get($variableName.text)[TYPE].equals("date")){
            if(symbolTable.get($variableName.text)[VALUE] != null){
              if($CONDITIONAL_OP.text.equals("=="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")==0";
            else if($CONDITIONAL_OP.text.equals("!="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")!=0";
            else if($CONDITIONAL_OP.text.equals("<="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")<=0";
            else if($CONDITIONAL_OP.text.equals(">="))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")>=0";
            else if($CONDITIONAL_OP.text.equals("<"))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")<0";
            else if($CONDITIONAL_OP.text.equals(">"))
              $val = "(" + $variableName.text +  ".compare(\"" + $DATE.text + "\")>0";
            }
            else{
              System.out.println("Error! No value assigned to variable!");
            }  
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
    }
   | STRINGTOK CONDITIONAL_OP variableName
     {
      if(symbolTable.containsKey($variableName.text)){
          if(symbolTable.get($variableName.text)[TYPE].equals("string")){
            if(symbolTable.get($variableName.text)[VALUE] != null){
            if($CONDITIONAL_OP.text.equals("=="))
              $val = $STRINGTOK.text +  "\".equalsIgnoreCase(" + $variableName.text + ")==0";
            else if($CONDITIONAL_OP.text.equals("!="))
              $val = $STRINGTOK.text +  "\".equalsIgnoreCase(" + $variableName.text + ")!=0";
            else if($CONDITIONAL_OP.text.equals("<="))
              $val = $STRINGTOK.text +  "\".compareToIgnoreCase(" + $variableName.text + ")<=0";
            else if($CONDITIONAL_OP.text.equals(">="))
              $val = $STRINGTOK.text +  "\".compareToIgnoreCase(" + $variableName.text + ")>=0";
            else if($CONDITIONAL_OP.text.equals("<"))
              $val = $STRINGTOK.text +  "\".compareToIgnoreCase(" + $variableName.text + ")<0";
            else if($CONDITIONAL_OP.text.equals(">"))
              $val = $STRINGTOK.text +  "\".compareToIgnoreCase(" + $variableName.text + ")>0";
            }
            else{
              System.out.println("Error!  No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
    }
  | variableName CONDITIONAL_OP STRINGTOK
    {
      if(symbolTable.containsKey($variableName.text)){
          if(symbolTable.get($variableName.text)[TYPE].equals("string")){
            if(symbolTable.get($variableName.text)[VALUE] != null){
            if($CONDITIONAL_OP.text.equals("=="))
              $val = $variableName.text +  ".equalsIgnoreCase(" + $STRINGTOK.text + ")==0";
            else if($CONDITIONAL_OP.text.equals("!="))
              $val = $variableName.text +  ".equalsIgnoreCase(" + $STRINGTOK.text + ")!=0";
            else if($CONDITIONAL_OP.text.equals("<="))
              $val = $variableName.text +  ".compareToIgnoreCase(" + $STRINGTOK.text + ")<=0";
            else if($CONDITIONAL_OP.text.equals(">="))
              $val = $variableName.text +  ".compareToIgnoreCase(" + $STRINGTOK.text + ")>=0";
            else if($CONDITIONAL_OP.text.equals("<"))
              $val = $variableName.text +  ".compareToIgnoreCase(" + $STRINGTOK.text + ")<0";
            else if($CONDITIONAL_OP.text.equals(">"))
              $val = $variableName.text +  ".compareToIgnoreCase(" + $STRINGTOK.text + ")>0";
            }
            else{
              System.out.println("Error!  No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mism atch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
      }
    }
  ;

assignmentStatement returns [String val]
  :
  DATET variableName ASSIGNMENT_OP DATE ENDOFSTMT
  {
    if(!symbolTable.containsKey($variableName.text))
    {
      String[] s = new String[2];
      s[0] = "date";
      s[1] = $DATE.text;
      symbolTable.put($variableName.text, s);
     // $val = "String " + $variableName.text + $ASSIGNMENT_OP.text + "\"" + $DATE.text +"\";";
     $val = "_date = \"" + $DATE.text + "\"; \n" +
            "java.util.Date "+ $variableName.text +"= null; \n" +
            "try{ \n" +
             $variableName.text +" = _sdf.parse(_date); \n" +
            " } catch (ParseException _e) { \n" +
            "} \n";
      
      date = $DATE.text;
      checkDate = null;
      sdf = new SimpleDateFormat("MM/dd");
    
    try {
      
      checkDate = sdf.parse(date);
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      System.out.println("EXCEPTION in date format!!!!!!!!!!!");
      
    }
    
    if(!sdf.format(checkDate).equals(date)){
      System.out.println("ERROR in date format!!!!");
    }
    }
    else
      System.out.println("Error! You are trying to redefine a previously defined variable " + $variableName.text);
  }
  |
  DATET variableName ENDOFSTMT
  {
    if(symbolTable.containsKey($variableName.text))
      System.out.println("Error! You are trying to redefine a previously defined variable " + $variableName.text);
    else {
      String[] s = new String[2];
      s[0] = "date";
      s[1] = null;
      symbolTable.put($variableName.text, s);
     // $val = "String " + $variableName.text + " = null;";
     $val = "java.util.Date "+ $variableName.text +"= null; \n";
    }
  }
  |
  variableName ASSIGNMENT_OP DATE ENDOFSTMT
  {
    if(!symbolTable.containsKey($variableName.text))
      System.out.println("Error! Undefined variable!!" + $variableName.text);
    else {
      if(symbolTable.get($variableName.text)[TYPE].equals("date")){
      String[] s = new String[2];
      s[0] = "date";
      s[1] = $DATE.text;
      symbolTable.put($variableName.text, s);
    //  $val = $variableName.text + " = new Date(\"" + $DATE.text + "\");" ;}
   $val = "_date = \"" + $DATE.text + "\"; \n" +
          "try{ \n" +
             $variableName.text +" = _sdf.parse(_date); \n" +
            " } catch (ParseException _e) { \n" +
            "} \n";
            
     date = $DATE.text;
      checkDate = null;
    sdf = new SimpleDateFormat("MM/dd");
    
    try { 
      
       checkDate = sdf.parse(date);
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      System.out.println("EXCEPTION in date format!!!!!!!!!!!");
      
    }
    
    if(!sdf.format(checkDate).equals(date)){
      System.out.println("ERROR in date format!!!!");
    }
    
    }
      else{
        System.out.println("Error! Type mismatch!");
      }
    }
  }
  | INT variableName ASSIGNMENT_OP NUMBER ENDOFSTMT
  {
    if(!symbolTable.containsKey($variableName.text)){
    String[] s = new String[2];
    s[0] = "int";
    s[1] = $NUMBER.text;
    symbolTable.put($variableName.text, s);
    $val = "int " + $variableName.text + $ASSIGNMENT_OP.text + $NUMBER.text + ";";}
    else
      System.out.println("Error! You are trying to redefine a previously defined variable " + $variableName.text);
  }
  |
  INT variableName ENDOFSTMT
  {
    if(symbolTable.containsKey($variableName.text))
      System.out.println("Error! You are trying to redefine a previously defined variable " + $variableName.text);
    else {
      String[] s = new String[2];
      s[0] = "int";
      s[1] = null;
      symbolTable.put($variableName.text, s);
      $val = "int " + $variableName.text + ";";
    }
  }
  |
  variableName ASSIGNMENT_OP NUMBER ENDOFSTMT
  {
    if(!symbolTable.containsKey($variableName.text))
      System.out.println("Error! Undefined variable!" + $variableName.text);
    else {
       if(symbolTable.get($variableName.text)[TYPE].equals("int")){
      String[] s = new String[2];
      s[0] = "int";
      s[1] = null;
      symbolTable.put($variableName.text, s);
      $val =  $variableName.text + " = " + $NUMBER.text + ";";}
      else{
        System.out.println("Error! Type mismatch!");
      }
    }
  }
  |
  STRING variableName ASSIGNMENT_OP STRINGTOK ENDOFSTMT
  {
    if(!symbolTable.containsKey($variableName.text)){
    String[] s = new String[2];
    s[0] = "string";
    s[1] = $STRINGTOK.text;
    symbolTable.put($variableName.text, s);
    $val = "String " + $variableName.text + $ASSIGNMENT_OP.text + $STRINGTOK.text + ";";}
    else
      System.out.println("Error! You are trying to redefine a previously defined variable " + $variableName.text);
  }
  |
  STRING variableName ENDOFSTMT
  {
    if(symbolTable.containsKey($variableName.text))
      System.out.println("Error! You are trying to redefine a previously defined variable " + $variableName.text);
    else {
      String[] s = new String[2];
      s[0] = "string";
      s[1] = null;
      symbolTable.put($variableName.text, s);
      $val = "String " + $variableName.text + ";";
    }
  }
  |
  variableName ASSIGNMENT_OP STRINGTOK ENDOFSTMT
  {
    if(!symbolTable.containsKey($variableName.text))
      System.out.println("Error! Undefined vairable!" + $variableName.text);
    else {
     if(symbolTable.get($variableName.text)[TYPE].equals("date")){
      String[] s = new String[2];
      s[0] = "string";
      s[1] = null;
      symbolTable.put($variableName.text, s);
      $val = $variableName.text + "=" + $STRINGTOK.text + ";";}
      else{
         System.out.println("Error! Type mismatch!");
      }
    }
  }
  |
   v1=variableName ASSIGNMENT_OP v2=variableName ENDOFSTMT
  {
    if(symbolTable.containsKey($v1.text) && symbolTable.containsKey($v2.text)){
          if(symbolTable.get($v1.text)[TYPE].equals(symbolTable.get($v2.text)[TYPE])){
            if(symbolTable.get($v2.text)[VALUE] == null){
              $val = $v1.text + " = " + $v2.text + ";" ;
          }
          else{
              System.out.println("Error!  No value assigned to variable!");
            }
          }
          else{
            System.out.println("Error! Type Mismatch");
          }
      }
      else{
        System.out.println("Error! Undefined variable name");
    }
  }
  |
  variableName ASSIGNMENT_OP READT LEFTPARAN RIGHTPARAN ENDOFSTMT
  {
    if(symbolTable.containsKey($variableName.text)){
      String[] s = new String[2];
      s = symbolTable.get($variableName.text);
      s[1] = "";
      symbolTable.put($variableName.text, s);
   //   $val = "String " + $variableName.text + ";";
             // $val = $variableName.text + " = s.next();";
             $val="_input=s.next();\n";
             if(s[0].equalsIgnoreCase("string"))
             {
             $val+=$variableName.text+"=_input;\n";
             }
             else if(s[0].equalsIgnoreCase("int"))
             {
             $val+=$variableName.text+"=Integer.parseInt(_input);\n";
             }
             else if(s[0].equalsIgnoreCase("date"))
             {
                $val += "_date = _input ; \n" +
                "try{ \n" +
                  $variableName.text +" = _sdf.parse(_date); \n" +
            " } catch (ParseException _e) { \n" +
            "} \n" +
            "if(!_sdf.format(" + $variableName.text + ").equals(_date)){ \n" +
            " System.out.println(\"ERROR in date format!!!!\"); \n" +
            " } \n "
            ;
            
            
         
            }
            
     
    }
  }
  ;

createEventCommand returns [String val]
  :
  CREATEEVENT LEFTPARAN parameters RIGHTPARAN ENDOFSTMT  
  {
    String s = new String("createEvent(" + $parameters.val + ")");
    mainFuncString.add(s);
    $val = "createEvent(" + $parameters.val + ")" + ";\n";
  }
  ;

runCommand returns [String val]
  :
  GENERATEOUTFIT LEFTPARAN parameters RIGHTPARAN ENDOFSTMT
  {
    String s = new String("generateOutfit(" + $parameters.val + ")");
    mainFuncString.add(s);
    $val = "generateOutfit(" + $parameters.val + ");";
  }
  ;

parameters  returns [String val]
  :
  IDENTIFIER parameters1
    {
//       System.out.println("text2: "+$IDENTIFIER.text);
       $val = $IDENTIFIER.text + ", " + $parameters1.param ;
       $val = $val.substring(0, $val.length()-2);
    }
  ;

parameters1 returns [String param]
  :
  ',' IDENTIFIER p1=parameters1
    {
//      System.out.println("text: "+$IDENTIFIER.text);
        $param = $IDENTIFIER.text + ", " + $p1.param;
    }
  |//null
  { 
    $param = "";
//    System.out.println ("i did come here"); 
  }
  ;


wardrobeName  :  IDENTIFIER  ;

prefRange  :  '1' | '2' | '3'  ;

value  :  IDENTIFIER  ;

variableName  :  IDENTIFIER  ;

EXPR    :    'expr'    ;

DATE  :  ('0'| '1')('0'..'9') ('\/') ('0'..'3') ('0'..'9')  ;

//TIME  :  ('0'..'2') ('0'..'9') (':') ('0'..'5') ('0'..'9')  ;

CREATET  :  'create'  ;

ITEMT  :  'item'  ;

CATEGORY  :   'category'   ;

WEIGHT  :  'weight'  ;

COLORT  :  'color'  ;

SHADET  :  'shade'  ;

MAKERT  :  'maker'  ;

PREFERENCET  :  'preference'  ;

WARDROBET  :  'wardrobe'  ;

USER  :  'user'  ;

AGE  :  'age'  ;

OCCUPATION  :  'occupation'  ;

USET  :  'use'  ;

IF  :  'if'  ;

ELSE  :  'else'  ;

VOID  :  'void'  ;

MAIN  :  'main'  ;

FOREACH  :  'for each'  ;

AND  :  'and'  ;

OR  :  'or'  ;

PRINTT : 'print' ;

READT : 'read' ;

CREATEEVENT  :  'createEvent'  ;

GENERATEOUTFIT  :  'generateOutfit'  ;

INCLUDE  :  'include'  ;

REJTOK  :  'reject'  ;

RETTOK  :  'return'  ;

DATET  :  'date'  ;

ENDDATE  :  'endDate'  ;

ENDTIME  :  'endTime'  ;

EVENT  :  'event'  ;

GARMENT  :  'garment'  ;

HUMIDITY  :  'humidity'  ;

INT  :  'int'  ;

STARTDATE  :  'startDate'  ;

STARTTIME  :  'startTime'  ;

STRING  :  'string'  ;

TEMPERATURE  :  'temperature'  ;

//TIMET  :  'time'  ;

WEATHER  :  'weather'  ;

LEFTPARAN  :  '('  ;

RIGHTPARAN  :  ')'  ;

LEFTCURLY  :  '{'  ;

RIGHTCURLY  :  '}'  ;
  
ASSIGNMENT_OP  :  '='  ;
  
CONDITIONAL_OP  :  ( '==' | '!=' | '<' | '>' | '<=' | '>=')  ;

IDENTIFIER  :  ('a'..'z'| 'A'..'Z')('a'..'z'| 'A'..'Z'| '0'..'9')*  ;

NUMBER  :  ('0'..'9')+  ;

QUOTE : '\"';

ENDOFSTMT  :  ';'  ;

STRINGTOK : QUOTE ('a'..'z'| 'A'..'Z'| '0'..'9'|'\|'|'\?'|'\.'|'='|'\+'|'\{'|'\}'|'\['|']'|'@'|'#'|'\$'|'%'|'\^'|'&'|'\*'|'\('|'\)'|'<'|'>'|','|'\\'|'/'|'-'|'_'|'!')+ QUOTE ;

DELIMITER  :  '.'  ;
  
 WS : (' ' | '\t' | '\n' | '\r' | '\f ')+ {$channel=HIDDEN;};